package org.dync.giftlibrary.adapter;


import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import org.dync.giftlibrary.ResourceTable;
import org.dync.giftlibrary.widget.GiftModel;

import java.io.IOException;
import java.util.List;

public class FaceGVAdapter extends BaseItemProvider {
    private List<GiftModel> list;
    private Context mContext;

    private ViewHodler mHolder;
    private int clickTemp = -1;

    //标识选择的Item
    public void setSeclection(int position) {
        clickTemp = position;
    }

    public int getSecletion() {
        return clickTemp;
    }

    public FaceGVAdapter(ListContainer recyclerView, List<GiftModel> list, Context mContext, boolean isNetData) {
        super();
        this.list = list;
        this.mContext = mContext;

    }

    public void clear() {
        this.mContext = null;
    }

    public void clearSelection() {
        if (mHolder != null) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setShape(ShapeElement.RECTANGLE);
            shapeElement.setCornerRadius(20);
            shapeElement.setDashPathEffectValues(new float[]{30, 30}, 1);
            shapeElement.setStroke(2, new RgbColor(235, 236, 237));
            mHolder.llroot.setBackground(shapeElement);
            mHolder = null;
        }else {
            clickTemp=-1;
            notifyDataChanged();
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        ViewHodler holder;
        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_face_image, null, false);
            holder = new ViewHodler(component);
            component.setTag(holder);
        } else {
            holder = (ViewHodler) component.getTag();
        }
        final GiftModel giftModel = list.get(position);
        component.setWidth(getScreenWidth(mContext) / 7);
        try {
            ResourceManager manager = mContext.getResourceManager();
            Resource resource1 = manager.getRawFileEntry(giftModel.getGiftName()).openRawFile();
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            ImageSource imageSource = ImageSource.create(resource1, sourceOptions);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            holder.giftImg.setPixelMap(pixelMap);
        } catch (IOException e) {
            e.printStackTrace();
        }

        holder.giftName.setText(giftModel.getGiftName());
        holder.giftPrice.setText(giftModel.getGiftPrice());
        if (clickTemp == position) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setShape(ShapeElement.RECTANGLE);
            shapeElement.setCornerRadius(5);
            shapeElement.setDashPathEffectValues(new float[]{15, 8}, 1);
            shapeElement.setStroke(2, new RgbColor(255, 140, 0));
            holder.bg.setBackground(shapeElement);
            mHolder = holder;
        } else {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setShape(ShapeElement.RECTANGLE);
            shapeElement.setCornerRadius(5);
            shapeElement.setDashPathEffectValues(new float[]{15, 8}, 1);
            shapeElement.setStroke(2, new RgbColor(235, 236, 237));
            holder.bg.setBackground(shapeElement);
        }
        Component finalComponent = component;
        holder.llroot.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component c) {
                ShapeElement shapeElement = new ShapeElement();
                shapeElement.setShape(ShapeElement.RECTANGLE);
                shapeElement.setCornerRadius(5);
                shapeElement.setDashPathEffectValues(new float[]{15, 8}, 1);
                shapeElement.setStroke(2, new RgbColor(255, 140, 0));
                holder.bg.setBackground(shapeElement);
                clickTemp = position;
                mOnItemClickListener.onItemClick(finalComponent, giftModel, position);
                notifyDataChanged();
            }
        });
        return component;
    }

    static class ViewHodler {
        DirectionalLayout llroot;
        DirectionalLayout bg;
        Image giftImg;
        Text giftName;
        Text giftPrice;

        public ViewHodler(Component view) {
            llroot = (DirectionalLayout) view.findComponentById(ResourceTable.Id_ll_gift_root);
            bg = (DirectionalLayout) view.findComponentById(ResourceTable.Id_bg);
            giftImg = (Image) view.findComponentById(ResourceTable.Id_face_img);
            giftName = (Text) view.findComponentById(ResourceTable.Id_face_name);
            giftPrice = (Text) view.findComponentById(ResourceTable.Id_face_price);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Component view, GiftModel giftModel, int position);
    }

    public OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    private int getScreenWidth(Context context) {
        return DisplayManager.getInstance().getDefaultDisplay(context).get().getRealAttributes().width;
    }
}
